package juez

class ClarificationController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
       /**intercepta la conexion llamando primero al metodo auth**/
    def beforeInterceptor = [action:this.&auth]


    def index = {
        redirect(action: "list", params: params)
    }
    
     /**impide el paso a menos que este logeado**/
      def auth() {
        if(!session.user) {
            redirect(controller:"user", action:"login")
            return false
        }
    }
    /**impide el paso a menos que este logeado**/

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [clarificationInstanceList: Clarification.list(params), clarificationInstanceTotal: Clarification.count()]
    }

    def create = {
        def clarificationInstance = new Clarification()
        clarificationInstance.properties = params
        return [clarificationInstance: clarificationInstance]
    }

    def save = {
        def clarificationInstance = new Clarification(params)
        if (clarificationInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'clarification.label', default: 'Clarification'), clarificationInstance.id])}"
            redirect(action: "show", id: clarificationInstance.id)
        }
        else {
            render(view: "create", model: [clarificationInstance: clarificationInstance])
        }
    }

    def show = {
        def clarificationInstance = Clarification.get(params.id)
        if (!clarificationInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clarification.label', default: 'Clarification'), params.id])}"
            redirect(action: "list")
        }
        else {
            [clarificationInstance: clarificationInstance]
        }
    }

    def edit = {
        def clarificationInstance = Clarification.get(params.id)
        if (!clarificationInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clarification.label', default: 'Clarification'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [clarificationInstance: clarificationInstance]
        }
    }

    def update = {
        def clarificationInstance = Clarification.get(params.id)
        if (clarificationInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (clarificationInstance.version > version) {
                    
                    clarificationInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'clarification.label', default: 'Clarification')] as Object[], "Another user has updated this Clarification while you were editing")
                    render(view: "edit", model: [clarificationInstance: clarificationInstance])
                    return
                }
            }
            clarificationInstance.properties = params
            if (!clarificationInstance.hasErrors() && clarificationInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'clarification.label', default: 'Clarification'), clarificationInstance.id])}"
                redirect(action: "show", id: clarificationInstance.id)
            }
            else {
                render(view: "edit", model: [clarificationInstance: clarificationInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clarification.label', default: 'Clarification'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def clarificationInstance = Clarification.get(params.id)
        if (clarificationInstance) {
            try {
                clarificationInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'clarification.label', default: 'Clarification'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'clarification.label', default: 'Clarification'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clarification.label', default: 'Clarification'), params.id])}"
            redirect(action: "list")
        }
    }
}
